package com.ita.acmsystem.merchant.servlet;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONException;

import com.ita.acmsystem.merchant.config.Constant;
import com.ita.acmsystem.merchant.entity.Merchant;
import com.ita.acmsystem.merchant.service.IMerchantService;
import com.ita.acmsystem.merchant.service.impl.MerchantServiceImpl;
import com.ita.acmsystem.merchant.util.UrlConnUtil;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private IMerchantService merchantService = new MerchantServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doLogin(request, response);
	}

	private void doLogin(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		String password = request.getParameter("password");

		Merchant user = merchantService.login(name, password);

		if (user != null) {
			String merchantId = user.getMerchantId();
			Map<String, String> map = null;
			try {
				map = UrlConnUtil
						.getLoginStatusFromAdmin(merchantId);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(map);
			if (String.valueOf(Constant.WHITE_LIST).equals(map.get("isBlack"))) {
				String auditStatus = map.get("auditStatus");
				if (String.valueOf(Constant.AUDIT_STATUS_PASS).equals(
						auditStatus)) {
					HttpSession session = request.getSession();
					session.setAttribute("user", user);

					// request.getRequestDispatcher("ContentServlet").forward(request,
					// response);
					response.sendRedirect("BootstrapWebContent/content.html");
				} else {
					HttpSession session = request.getSession();
					session.setAttribute("user", user);
					
					switch (auditStatus) {
					case "1":
						request.setAttribute("auditRemark", map.get("remarks"));
						break;
					case "2":
						request.setAttribute("auditRemark", "审核中");
						break;
					}

					request.getRequestDispatcher("/reAudit.jsp").forward(
							request, response);
				}
			} else if (String.valueOf(Constant.BLACK_LIST).equals(map.get("isBlack"))) {
				request.setAttribute("registerMsg", "You have been defriended");
				request.getRequestDispatcher("/register.jsp").forward(request,
						response);
			} else {
				request.setAttribute("loginMsg", "Admin Part has shut down");
				request.getRequestDispatcher("/index.jsp").forward(request,
						response);
			}
		} else {
			request.setAttribute("loginMsg", "Pls Register First");
			request.getRequestDispatcher("/index.jsp").forward(request,
					response);
		}
	}
}
